#include "senderui.h"
#include "ui_senderui.h"

#include <libmqttclient/syncpublish.h>
#include <common.h>
#include <uavtmi/uavtmi.pb.h>

#include <QDebug>
#include <QSettings>


SenderUI::SenderUI(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SenderUI)
{
    ui->setupUi(this);
    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotTimeOut()));
    init();

    m_publishCli = new SyncPublish();
    connect(m_publishCli, SIGNAL(commandControl(QByteArray, QString*)), this, SIGNAL(commandToF(QByteArray, QString*)));
    connect(m_publishCli, SIGNAL(commandControl(QByteArray, QString*)), this, SLOT(slotCommandToF(QByteArray, QString*)));


    QSettings settings("client_settings.ini", QSettings::IniFormat);
    qDebug() << settings.status();
    m_publishCli->setAdress(settings.value("ServerUrl", "tcp://localhost:1883").toString().toStdString());
    m_publishCli->setClientId(settings.value("ClientID", "SyncPublisher").toString().toStdString());
    m_publishCli->setTopic(settings.value("Topic", "hello").toString().toStdString());
    m_publishCli->setQOS(getQOSFromString(settings.value("QOS", "FIRE_AND_FORGET").toString()));


    m_publishCli->activate();
}

SenderUI::~SenderUI()
{
    delete m_publishCli;
    delete ui;
}

void SenderUI::on_pbStart_clicked()
{
    m_timer->start(1000 / ui->spinBox->value());
    m_publishCli->activate();
}

void SenderUI::on_pbStop_clicked()
{
    m_timer->stop();
    m_publishCli->deactivate();
}

void SenderUI::init()
{
    for(uint i = uavtable::X; i < uavtable::LASTROW; i++)
    {
        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(rand()%100));
        newItem->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i,0, newItem);
    }
}

void SenderUI::slotTimeOut()
{
    updateTableDatas();
    sendDatas();
}

void SenderUI::updateTableDatas()
{
    for(uint i = uavtable::X; i < uavtable::LASTROW; i++)
    {
        int id = ui->tableWidget->item(i,0)->data(0).toInt();
        ui->tableWidget->item(i,0)->setData(0, ++id);
    }
}

void SenderUI::sendDatas()
{
    telemetry::Navigation navigation;
    navigation.set_x(ui->tableWidget->item(uavtable::X,0)->data(0).toDouble());
    navigation.set_y(ui->tableWidget->item(uavtable::Y,0)->data(0).toDouble());
    navigation.set_z(ui->tableWidget->item(uavtable::Z,0)->data(0).toDouble());
    navigation.set_altitude(ui->tableWidget->item(uavtable::ALTITUDE,0)->data(0).toDouble());
    navigation.set_height(ui->tableWidget->item(uavtable::HEIGHT,0)->data(0).toDouble());

    telemetry::UAVState uavState;
    uavState.set_redline(ui->tableWidget->item(uavtable::REDLINE,0)->data(0).toUInt());
    uavState.set_removal(ui->tableWidget->item(uavtable::REMOVAL,0)->data(0).toDouble());
    uavState.set_voltage(ui->tableWidget->item(uavtable::VOLTAGE,0)->data(0).toDouble());

    telemetry::Orientation uavOrientation;
    uavOrientation.set_displacement(ui->tableWidget->item(uavtable::DISPLACMENT,0)->data(0).toDouble());
    uavOrientation.set_pitch(ui->tableWidget->item(uavtable::PITCH,0)->data(0).toDouble());
    uavOrientation.set_roll(ui->tableWidget->item(uavtable::ROLL,0)->data(0).toDouble());
    uavOrientation.set_rate(ui->tableWidget->item(uavtable::RATE,0)->data(0).toDouble());

    std::string message;
    navigation.SerializeToString(&message);

    if(m_publishCli)
    {
      //  message = "hello from alexey!1";
        m_publishCli->sendMessage(message);
    }

//    telemetry::Navigation outNavigation;
//    outNavigation.ParseFromString(message);
//    qDebug() << outNavigation.ByteSize();

//    outNavigation.PrintDebugString();
}

publish::QOS SenderUI::getQOSFromString(QString str_qos)
{
    publish::QOS ret_qos = publish::FIRE_AND_FORGET; // what?!

    if(str_qos == "FIRE_AND_FORGET")
        ret_qos = publish::FIRE_AND_FORGET;
    else if(str_qos == "AT_LEAST_ONCE")
        ret_qos = publish::AT_LEAST_ONCE;
    else if(str_qos == "ONCE_AND_ONE_ONLY")
        ret_qos = publish::ONCE_AND_ONE_ONLY;

    return ret_qos;
}

void SenderUI::slotTelemetryFromF(const QString& topic, QByteArray ba)
{
    std::string send_string(ba.data(), ba.size());
    m_publishCli->sendMessage(topic, send_string);
}

void SenderUI::slotMsgResponceFromF(QString responce)
{
    m_publishCli->sendMessage("controllcommand_responce", responce.toStdString());
}


void SenderUI::slotCommandToF(QByteArray ba, QString* resp)
{
    qDebug() << "qDebug()";
  //  emit commandToF(ba, resp);
}

